package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaMaterialDao;
import com.qymerge.manage.base.dao.IMetaMaterialGroupDao;
import com.qymerge.manage.base.ro.MetaMaterialGroupRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: CardDaoImpl</p>
 *<p>Description: 优惠券管理</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:54
 */
@Repository
public class MetaMaterialGroupDaoImpl extends BaseDao implements IMetaMaterialGroupDao {

    private String dwd_MCRS_MDM_meta_MaterialGroup = "SELECT FID,FNumber,FName,FParentID FROM dwd_MCRS_MDM_meta_MaterialGroup WHERE 1=1 ";

    @Override
    public List list(MetaMaterialGroupRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_MaterialGroup;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getFID())) {
            sql += " and FID=:FID";
            paramMap.put("FID", ro.getFID());
        }

        if (StringUtils.isNotBlank(ro.getFNumber())) {
            sql += " and FNumber like :FNumber";
            paramMap.put("FNumber", "%" + ro.getFNumber() + "%");
        }

        if (StringUtils.isNotBlank(ro.getFName())) {
            sql += " and FName like :FName";
            paramMap.put("FName", "%" + ro.getFName() + "%");
        }

        if (ro.getFDeletedStatus() > -1) {
            sql += " and FDeletedStatus=:FDeletedStatus";
            paramMap.put("FDeletedStatus", ro.getFDeletedStatus());
        }

        // 分页数据获取
        sql += " order by FID desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaMaterialGroupRO>(MetaMaterialGroupRO.class));
    }

    @Override
    public int total(MetaMaterialGroupRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_MaterialGroup where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getFID())) {
            sql += " and FID=:FID";
            paramMap.put("FID", ro.getFID());
        }

        if (StringUtils.isNotBlank(ro.getFNumber())) {
            sql += " and FNumber like :FNumber";
            paramMap.put("FNumber", "%" + ro.getFNumber() + "%");
        }

        if (StringUtils.isNotBlank(ro.getFName())) {
            sql += " and FName like :FName";
            paramMap.put("FName", "%" + ro.getFName() + "%");
        }

        if (ro.getFDeletedStatus() > -1) {
            sql += " and FDeletedStatus=:FDeletedStatus";
            paramMap.put("FDeletedStatus", ro.getFDeletedStatus());
        }



        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaMaterialGroupRO findOne(String id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_MaterialGroup);
        sql.append("and FID=?");
        RowMapper<MetaMaterialGroupRO> rowMapper = new BeanPropertyRowMapper<MetaMaterialGroupRO>(MetaMaterialGroupRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }


}
